package com.wanshi.controller;

import com.github.pagehelper.PageInfo;
import com.wanshi.entity.Cust;
import com.wanshi.entity.CustVo;
import com.wanshi.entity.JsonResult;
import com.wanshi.service.CustService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class CustController {

    @Autowired
    private CustService custService;

    @RequestMapping("/list")
    public String list(Model model){
        List<Cust> list = custService.list();
        model.addAttribute("list",list);
        return "list";
    }

    //跳转添加
    @GetMapping("/addUser")
    public String addUser(){
        return "adduser";
    }


    //添加
    @PostMapping("/addUser")
    public String addUser(Cust cust,Model model){

        Map<String, String> errorMap = cust.checkUser();
        if(errorMap.size()>0){
            model.addAttribute("map",errorMap);
            model.addAttribute("user",cust);
            return "adduser";
        }else {
            custService.addUser(cust);
            return "index";
        }

    }

    // 分页
    @RequestMapping("/pageByCond")
    public String pageByCond(Cust cust,int page,int size,Model model){
        PageInfo pageInfo = custService.pageByCond(page, size, cust);
        model.addAttribute("page",pageInfo);
        return "pageCond";
    }

    //条件查询
    @RequestMapping("/findByCond")
    public String findByCond(Cust cust,Model model){
        List<Cust> list = custService.findByCond(cust);
        model.addAttribute("list",list);
        return "list";
    }

    //删除客户
    @RequestMapping("/delUser")
    @ResponseBody
    public JsonResult delUser(int id){
        int i = custService.delUser(id);
        JsonResult result = null;
        if(i == 0){
            result = new JsonResult(500,"删除删除!");
        }else {
            result = new JsonResult(200,"删除成功!");
        }
        return result;
    }


    //批量删除
    @RequestMapping("/delBatch")
    public String delBatch(String[] ids){
        if(ids == null || ids.length == 0){
            return "redirect:list";
        }
        custService.delBatch(ids);
        return "redirect:list";
    }


    //根据id获取用户
    @RequestMapping("/findUserById")
    public String findUserById(int id,Model model){
        Cust cust = custService.findUserById(id);
        model.addAttribute("cust",cust);
        return "update";
    }

    //修改
    @PostMapping("/update")
    public String update(Cust cust){
        custService.update(cust);
        return "redirect:list";
    }


    //批量修改跳转页面
    @GetMapping("/updateBatch/list")
    public String updateBatch(Model model){
        List<Cust> list = custService.list();
        model.addAttribute("list",list);
        return "updateBatch";
    }

    //批量修改
    @RequestMapping("/updateBatch")
    public String updateBatch(CustVo custVo){
        custService.updateBatch(custVo.getList());
        return "redirect:list";
        //return "index";
    }








}
